package cn.bgs.controller;

import cn.bgs.pojo.EmployeeInfo;
import cn.bgs.pojo.vo;
import cn.bgs.service.RegisterService;
import cn.bgs.utils.MyContent;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class RegisterController {
    Logger log= LogManager.getLogger();

    @Autowired
    private RegisterService registerService;

    @RequestMapping("/registerJsp")
    public String registerJsp(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(vo vo1) {


        registerService.addEmployee(vo1);
        registerService.addemployeeName(vo1);
        return "redirect:/loginJsp";

    }

    @RequestMapping("/findInfo")
    @ResponseBody
    public Map<String,Object> findInfo(@RequestParam(required=false,defaultValue="1") int page,
                                       @RequestParam(required=false,defaultValue="7") int limit){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<EmployeeInfo> info  = registerService.findInfo();// 这是我们的sql
        // 使用pageInfo包装查询
        PageInfo<EmployeeInfo> rolePageInfo = new PageInfo<>(info);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        return map;
    }


    @RequestMapping("/testAjax")
    public String testAjax(){
        return "testAjax";
    }


    @ResponseBody
    @RequestMapping("/helloAjax")
    public Map<String, Object> helloAjax(@RequestBody String str) {

        log.info("helloAjax --------"+str);
        log.info("Ajaxtest   json --------" + str);
        JSONObject jsonObject= JSONObject.parseObject(str);

        JSONObject response=jsonObject.getJSONObject("response");
        
        JSONObject data=response.getJSONObject("data");

        JSONArray jsonArray = data.getJSONArray("cnameRecords");




        for(int i=0;i<jsonArray.size();i++){
            JSONObject p = jsonArray.getJSONObject(i);
            if(p.getString("aliasHost").contains("webmail")){
                log.info("aliasHost:::::"+p.getString("aliasHost"));
                log.info("canonicalHost:::::"+p.getString("canonicalHost"));
            }
        }


        /*
        String company_name=jsonObject.getString("company_name");
        String level=jsonObject.getString("level");
        String url=jsonObject.getString("url");

        JSONArray jsonArr=jsonObject.getJSONArray("dept");
        for(int i=0;i<jsonArr.size();i++){
            JSONObject p = jsonArr.getJSONObject(i);
            if("女".equals(p.getString("sex"))){
                log.info("name:::::"+p.getString("name"));
                log.info("sex:::::"+p.getString("sex"));
                log.info("url:::::"+p.getString("url"));
            }
        }*/
        /*
        log.info("Ajaxtest   company_name --------" + company_name);
        log.info("Ajaxtest   level --------" + level);
        log.info("Ajaxtest   age --------" + age);
        log.info("Ajaxtest   dept --------" + dept);*/








        Map<String, Object> m = new HashMap<>();

        m.put(MyContent.CODE, "0000");
        m.put(MyContent.MSG, "交易成功");
        return m;
    }




}
